<?php

class Database {

	//konstan variabel
	const DB_HOST = 'localhost';
	const DB_USER = 'root';
	const DB_PASS = '';
	const DB_TUK = 'tuk_ijin_ln';
	const DB_KEPEG = 'sqldb12';
	const DB_PORT_KEPEG = '3306';
	
	
	//protected variabel
	protected $_connection_tuk;
	protected $_connection_kepeg;
	protected $mysqli_exception;
	protected $host;
	protected $user;
	protected $port;
	protected $password;
	protected $db_name;
	
	/**
	*protected konfigurasi
	*@var array
	*/
	protected $config_tuk;
	protected $config_kepeg;

	//simpan single instance
	private static $_instance;
	protected static $log;

	/**
	* Get singleton instance dari database
	* @return type database
	*/
	public static function getInstance() {
		if(!self::$_instance) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	
	/**
	* Constructor
	*/
	protected function __construct() {}

	/**
	* Konfigurasi sirekan
	* @return config array
	*/
	protected function configTuk() {
		if (isset($this->_connection_tuk)) {
			error_log('DATABASE CONNECTION::warning, koneksi ke database sirekan sudah dibuat');
		}

		$this->config_tuk = array(
			'host' => self::DB_HOST,
			'user' => self::DB_USER,
			'password' => self::DB_PASS,
			'db_name' => self::DB_TUK
		);
	}
	
	/**
	* Konfigurasi db vera
	* @return config array
	*/
	protected function configKepeg() {
		if (isset($this->_connection_kepeg)) {
			error_log('DATABASE CONNECTION::warning, koneksi ke database sp2d sudah dibuat');
		}

		$this->config_kepeg = array(
			'host' => self::DB_HOST,
			'user' => self::DB_USER,
			'port' => self::DB_PORT_KEPEG,
			'password' => self::DB_PASS,
			'db_name' => self::DB_KEPEG
		);
	}
	
	/**
	* Membuat koneksi mysqli
	* @return PDO/mysqli object
	*/	
	protected function createConnection($config=array()){
		
		if(is_array($config)) {
			error_log('DATABASE CONNECTION::warning, konfigurasi koneksi database tidak ada ');
		}
		
		if(count($config)>0) {
			foreach($config as $name => $value) {
				$this->$name=$value;
			}
		}
		
		//create new PDO Conncetion
		try {
			$new_connection= new PDO('mysql:host='.$this->host.';dbname='.$this->db_name, $this->user, $this->password);
		}catch(PDOException $e) {
			error_log('DATABASE CONNECTION::warning, gagal terkoneksi ke database. '.print_r($e, true));
			return false;
		}
		
		/**
		*jika menggunakan mysqli
		$new_connection=new mysqli($this->host,$this->user,$this->password,$this->db_name);
		
		if(mysqli_connect_error()) {
			error_log('DATABASE CONNECTION:warning, gagal terkoneksi dengan database');
			return false;
		}
		*/
		return $new_connection;
	}


	/**
	* Empty clone, magic to prevent duplication
	*/
	private function __clone() {}

	/**
	* Get Mysqli connection
	* @param int
	* @return PDO/mysqli Connection
	*/
	public function getConnection($sel=null)
	{
		if(isset($sel)) {
			if($sel==1){
				$this->configTuk();
				$this->_connection_tuk=$this->createConnection($this->config_tuk);
				return $this->_connection_tuk;
			} else if($sel==2) {
				$this->configKepeg();
				$this->_connection_kepeg=$this->createConnection($this->config_kepeg);
				return $this->_connection_kepeg;
			}
			else {
				error_log('DATABASE CONNECTION::warning, database selection failed');
			}
		}
		else {
			error_log('DATABASE CONNECTION::warning, database selection failed');
		}
	}
}